雅典軍隊如果有 Slack 通知,斐迪庇得斯就不用趕著從馬拉松跑回雅典了。
在持續集成和持續部署 (CI/CD) 流程中,及時了解工作流程的狀態對於快速響應和改進至關重要。本文將介紹如何在 GitHub Actions 工作流程中集成 Slack 通知服務,以便在工作流程運行時獲得實時通知,以及如何生成報告文件來跟蹤工作流程的結果。
今日範例程式: https://github.com/Edit-Mr/2024-GitHub-Actions/tree/main/26
今天的文章假設你會使用 Slack,並且已經有一個 Slack 工作區。如果你還沒有,可以參考 Slack 官方文檔 來創建一個。
創建 Slack 應用
Create New App。From scratch,並輸入應用的名稱和所屬工作區。Create App。設置 Incoming Webhook
Incoming Webhooks。Activate Incoming Webhooks。Add New Webhook to Workspace。Allow。創建工作流程文件
在 .github/workflows 目錄下創建一個新的工作流程文件,例如 notify-slack.yml,並加入以下內容:
name: Notify Slack
on:
push:
branches:
- main
pull_request:
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Notify Slack
uses: slackapi/slack-github-action@v1
with:
slack-token: ${{ secrets.SLACK_TOKEN }}
channel-id: "C1234567890" # 替換為你的頻道 ID
text: "工作流程已完成!查看詳細信息:[工作流程鏈接](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})"
解釋:
slack-token: 使用 GitHub Secrets 存儲你的 Slack Token。channel-id: 你的 Slack 頻道 ID。text: 訊息文本,包括工作流程鏈接。配置 GitHub Secrets
將你的 Slack Token 添加到 GitHub Secrets:
Settings > Secrets and variables > Actions。New repository secret。SLACK_TOKEN 的 secret,並填入你的 Slack Token。創建報告生成腳本
在 src 目錄下創建一個腳本文件,例如 generate-report.js,並添加以下內容:
import fs from "fs";
import path from "path";
const reportFilePath = path.join(__dirname, "report.md");
// 模擬生成報告的內容
const reportContent = `
# 工作流程報告
**日期**: ${new Date().toLocaleDateString()}
## 總結
本次工作流程運行成功。
## 詳細信息
- [工作流程鏈接](https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID})
`;
// 將報告內容寫入文件
fs.writeFileSync(reportFilePath, reportContent, "utf8");
console.log("報告已生成");
更新工作流程文件
在 .github/workflows 目錄下創建流程文件,例如 generate-report.yml,並加入以下內容:
name: Generate Report
on:
push:
branches:
- main
pull_request:
jobs:
report:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "20"
- name: Install dependencies
run: npm install
- name: Generate report
run: node src/generate-report.js
- name: Commit and push report
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Actions"
git add report.md
git commit -m "📝 Update report"
git push
解釋:
Generate report: 運行生成報告的腳本。Commit and push report: 將生成的報告文件提交到 repository。在本文中,我們介紹了如何在 GitHub Actions 工作流程中配置 Slack 通知,以便實時了解工作流程的狀態,並如何生成報告文件以跟蹤工作流程的結果。當然今天的練習非常的簡單,你可以根據實際需求進行擴展,例如添加更多通知類型、生成更複雜的報告等。